package com.ruoyi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.FaBmdEntryApi;
import com.ruoyi.domain.FaBmdtypeApi;
import com.ruoyi.domain.FaBmdtypeApiVo1;
import com.ruoyi.mapper.FaBmdEntryApiMapper;
import com.ruoyi.mapper.FaBmdtypeApiMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@SaCheckLogin
@Api(tags = "骨密度词条")
@RequestMapping("/ray/bmdentry")
public class ApiBmdEntryController extends BaseController {

    @Autowired
    private FaBmdtypeApiMapper faBmdtypeApiMapper;

    @Autowired
    private FaBmdEntryApiMapper faBmdEntryApiMapper;

    @GetMapping("/get_bmdtype_list")
    @ApiOperation(value = "获取骨密度类型",position = 18)
    public TableDataInfo get_bmdtype_list() {
//        QueryWrapper<FaBmdtypeApi> wp = new QueryWrapper();
//        wp.orderByDesc("weigh");

        List<FaBmdtypeApi> list = faBmdtypeApiMapper.selectList(new QueryWrapper<FaBmdtypeApi>().orderByDesc("weigh"));
        return getDataTable(list);
    }

    @GetMapping("/get_bmd_entry_list")
    @ApiOperation(value = "获取骨密度词条",position = 19)
    public TableDataInfo get_bmd_entry_list(Integer bmdtype_id) {
        QueryWrapper<FaBmdEntryApi> wp = new QueryWrapper();
        if (bmdtype_id != null) {
            wp.eq("bmdtype_id", bmdtype_id);
        } else {

        }

        wp.orderByDesc("weigh");
        List<FaBmdEntryApi> list = faBmdEntryApiMapper.selectList(wp);
        return getDataTable(list);
    }

    @GetMapping("/get_bmdtype_list2")
    @ApiOperation(value = "获取骨密度类型和词条",position = 20)
    public AjaxResult get_bmdtype_list_and_entry() {
        QueryWrapper<FaBmdtypeApi> wp = new QueryWrapper();
        wp.orderByDesc("weigh");


        List<FaBmdtypeApi> type_list = faBmdtypeApiMapper.selectList(new QueryWrapper<FaBmdtypeApi>().orderByDesc("weigh"));

        List<FaBmdtypeApiVo1> list = new ArrayList<>();


        for (FaBmdtypeApi res : type_list) {

            FaBmdtypeApiVo1 temp = new FaBmdtypeApiVo1();
            BeanUtil.copyProperties(res, temp);//用糊涂工具可以简写

            //            temp.setId(res.getId());
            //            temp.setName(res.getName());
            //            temp.setWeigh(res.getWeigh());
            List<FaBmdEntryApi> lista = faBmdEntryApiMapper.selectList(new QueryWrapper<FaBmdEntryApi>().eq("bmdtype_id", temp.getId()));
            temp.setList(lista);

            try {
                System.out.println(temp);
                list.add(temp);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
        return AjaxResult.success("", list);
    }

    @PostMapping("/add_bmdtype")
    @ApiOperation(value = "添加骨密度类型",position = 26)
    public AjaxResult add_bmdtype(FaBmdtypeApi faBmdtype) {
        QueryWrapper<FaBmdtypeApi> wp = new QueryWrapper();
        wp.eq("name", faBmdtype.getName());
        FaBmdtypeApi res = faBmdtypeApiMapper.selectOne(wp);
        if (res != null) {
            return AjaxResult.error("已有" + faBmdtype.getName());
        }

        Integer insert = faBmdtypeApiMapper.insert(faBmdtype);
        if (insert > 0)
            return AjaxResult.success("添加成功", faBmdtype);
        return AjaxResult.error("添加失败");
    }

    @PostMapping("/update_bmdtype")
    @ApiOperation(value = "更新骨密度类型",position = 27)
    public AjaxResult update_update(FaBmdtypeApi faBmdtype) {
        QueryWrapper<FaBmdtypeApi> wp = new QueryWrapper();
        wp.eq("id", faBmdtype.getId());
        FaBmdtypeApi res = faBmdtypeApiMapper.selectOne(wp);
        if (res != null) {
            Integer update = faBmdtypeApiMapper.updateById(faBmdtype);
            if (update > 0)
                return AjaxResult.success("更新成功", faBmdtype);
            return AjaxResult.error("更新失败");

        } else {
            return AjaxResult.error("没有该内容");
        }
    }


    @PostMapping("/del")
    @ApiOperation(value = "删除骨密度类型",position = 28)
    public AjaxResult del(Integer id) {
        Integer res = faBmdtypeApiMapper.deleteById(id);
        if (res > 0) {
            return AjaxResult.success("删除成功", id);
        } else {
            return AjaxResult.error("没有该内容");
        }
    }


    @PostMapping("/add_bmdentry")
    @ApiOperation(value = "添加骨密度词条", position = 30)
    public AjaxResult add_bmdentry(FaBmdEntryApi faBmdEntryApi) {
        QueryWrapper<FaBmdEntryApi> wp = new QueryWrapper();
        wp.eq("name", faBmdEntryApi.getName());
        FaBmdEntryApi res = faBmdEntryApiMapper.selectOne(wp);
        if (res != null) {
            return AjaxResult.error("已有" + faBmdEntryApi.getName());
        }

        Integer insert = faBmdEntryApiMapper.insert(faBmdEntryApi);
        if (insert > 0)
            return AjaxResult.success("添加成功", faBmdEntryApi);
        return AjaxResult.error("添加失败");
    }

    @PostMapping("/update_bmdentry")
    @ApiOperation(value = "更新骨密度词条", position = 31)
    public AjaxResult update_bmdentry(FaBmdEntryApi faBmdEntryApi) {
        QueryWrapper<FaBmdEntryApi> wp = new QueryWrapper();
        wp.eq("id", faBmdEntryApi.getId());
        FaBmdEntryApi res = faBmdEntryApiMapper.selectOne(wp);
        if (res != null) {
            Integer update = faBmdEntryApiMapper.updateById(faBmdEntryApi);
            if (update > 0)
                return AjaxResult.success("更新成功", faBmdEntryApi);
            return AjaxResult.error("更新失败");

        } else {
            return AjaxResult.error("没有该内容");
        }
    }


    @PostMapping("/del_bmdentry")
    @ApiOperation(value = "删除骨密度词条",position = 32)
    public AjaxResult del_bmdentry(Integer id) {
        Integer res = faBmdEntryApiMapper.deleteById(id);
        if (res > 0) {
            return AjaxResult.success("删除成功", id);
        } else {
            return AjaxResult.error("没有该内容");
        }
    }


}
